import { scrollMixin } from "../../../utils/mixin";

export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      keyword: "",
      activeName: 0,
      dataList: [],
      titleArr: [],
      isLoadMore: true,

      loading: false
    };
  },
  methods: {
    toUrl(name, params = {}, query = {}) {
      this.$router.push(this.fun.getUrl(name, params, query));
    },
    init() {
      this.dataList = [];
      this.titleArr = [
        { id: 0, name: "全部", url: 'refund.list.index' },
        { id: 1, name: "待处理", url: 'refund.list.wait' },
        { id: 2, name: "已完成", url: 'refund.list.complete' },
        { id: 3, name: "驳回/关闭", url: 'refund.list.cancel' }
      ];
      this.isLoadMore = true;
      this.setNewArr();
    },
    setNewArr() {
      this.titleArr.forEach((val, index) => {
        this.dataList.push({
          isLoadMore: true,
          loading: false,
          finished: false,
          page: 1,
          total_page: 0,
          url: val.url,
          list: []
        });
      });
      this.handleClick();
    },
    onLoad() { },
    onSearch() {
      this.dataList[this.activeName].list = [];
      this.dataList[this.activeName].page = 1;
      this.handleClick();
    },
    handleClick() {
      let _item = this.dataList[this.activeName];
      if (!this.fun.isTextEmpty(_item.list)) {
        this.isLoadMore = _item.isLoadMore;
        return;
      }
      let { refund_id = '', order_goods_id = '' } = this.$route.query;

      $http
        .get(_item.url, { page: 1, sn: this.keyword, refund_id: refund_id, order_goods_id: order_goods_id }, ".")
        .then(response => {
          if (response.result === 1) {
            this.isLoadMore = true;
            this.dataList[this.activeName].isLoadMore = true;
            this.dataList[this.activeName].page = 1;
            this.dataList[this.activeName].total_page = response.data.last_page;
            this.$set(this.dataList[this.activeName], "list", response.data.data);
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.dataList[that.activeName].isLoadMore = false;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (that.dataList[that.activeName].page >= that.dataList[that.activeName].total_page) {
        return;
      } else {
        let { refund_id = '', order_goods_id = '' } = this.$route.query;
        let _json = {
          sn: this.keyword, refund_id: refund_id, order_goods_id: order_goods_id
        };
        that.dataList[that.activeName].page = that.dataList[that.activeName].page + 1;
        _json.page = that.dataList[that.activeName].page;

        $http.get(this.dataList[this.activeName].url, _json, "加载中").then(
          response => {
            that.isLoadMore = true;
            that.dataList[that.activeName].isLoadMore = true;
            if (response.result === 1) {
              var myData = response.data.data;
              that.dataList[that.activeName].list = that.dataList[that.activeName].list.concat(myData); //数组拼接
            } else {
              that.dataList[that.activeName].page = that.dataList[that.activeName].page - 1;
              that.dataList[that.activeName].isLoadMore = false;
              that.isLoadMore = false;
            }
          }
        );
      }
    },
    // operation(btn, params) {
    //   let { id } = params;
    //   if (btn.value == 2) {
    //     this.$router.push(this.fun.getUrl("express", { refund_id: id }));
    //   } else if (btn.value == 1) {
    //     this.$router.push(this.fun.getUrl("refund", { order_id: id, order_type: 2 }));
    //   } else if (btn.value == 6) {
    //     if (this.myData.resend_express_id == 0) {
    //       this.$router.push(
    //         this.fun.getUrl(
    //           "multiplePackages",
    //           {
    //             order_id: id //传送订单id到多包裹页面
    //           },
    //           {
    //             from: "resend"
    //           }
    //         )
    //       );
    //     } else {
    //       this.$router.push(
    //         this.fun.getUrl(
    //           "logistics",
    //           {},
    //           {
    //             refund_id: id,
    //             express_id: this.myData.resend_express_id,
    //             from: "resend"
    //           }
    //         )
    //       );
    //     }
    //   } else {
    //     //走默认请求按钮api
    //     this.getBtnApi(btn.api, params);
    //   }
    // },
    async getBtnApi(_url, params) {
      let { id } = params;
      let { result, msg, data } = await $http.get(_url, { refund_id: id }, ".");
      this.$dialog.alert({ message: msg }).then(() => {
        if (result == 1) this.onSearch();
      });
    },
    toDetail(item) {
      if (item.plugin_id == 40 && item.refund_type == 1 && item.status == 6) {
        this.toUrl('OrderReturnAftersalesDetail', { id: item.order.id })
      } else {
        if (item.plugin_id === 144) this.toUrl('refundProcess', {}, { refundId: item.id });
        if (item.plugin_id !== 144) this.toUrl('aftersales', { refund_id: item.id }, { pluginid: item.plugin_id })
      }
    }
  },
  activated() {
    this.init();
  },
  components: {}
};
